<?php

namespace app\index\model;

use app\admin\model\Role;
use think\db\Query;
use think\facade\Db;
use think\facade\Session;
use think\Model;
use think\model\concern\SoftDelete;

class Comment extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    public function userInfo()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }
    /**
     * @param int user_id 用户ID（可选）
     * @param int article_id 文章ID （可选）
     *  */
    public function list($data = [])
    {

        $limit = isset($data['limit']) ? $data['limit'] : 5;
        if (array_key_exists('user_id', $data)) {
            $pageQuery = array_key_exists('user_id', $data) ? ['list_rows' => $limit, 'query' => ['uid' => $data['user_id']]] : $limit;
        } else {
            $pageQuery = array_key_exists('article_id', $data) ? ['list_rows' => $limit, 'query' => ['art_id' => $data['article_id']]] : $limit;
        }
        $list = $this->withTrashed()->with(['userInfo'  => function (Query $query) {
            $query->field('id,nickname,head_icon,role');
        }])->where($data)->order('id asc')->paginate($pageQuery);
        // 悬赏模式 评论显示方式 - 1所有人可见 2仅自己可见
        if (array_key_exists('article_id', $data)) {
            $artData = Article::where('id', $data['article_id'])->find();
            if ($artData['type'] == 3) {
                $adopt = Db::name('artAdopt')->where('article_id', $data['article_id'])->find();
                if ($adopt) {
                    // 默认为所有人可看
                    $comment_show_type = $adopt['type'] ?? 1;
                }
            }
        }
        foreach ($list as $k => $item) {
            $role = Role::getRoleName($item->userInfo->role);
            $list[$k]['role'] = $role  == 'all,' ? '站长' : $role;
            if($item['delete_time'])
            {
                $list[$k]['comment'] = '作者被禁止或删除 内容自动屏蔽';
            }
            if (array_key_exists('article_id', $data) && $artData['type'] == 3) {
                if ($comment_show_type == 2 && $artData->user_id == Session::get('user.user_id')) {
                    $list[$k]['comment'] = $list[$k]['comment'];
                }
                // 评论者自身评论可见 （ 普通用户）
                else if ($comment_show_type == 2 && Session::get('user.user_id') != $item->user_id && $artData->user_id != $item->user_id) {
                    $list[$k]['comment'] = '仅发布者可见';
                } else {
                    $list[$k]['comment'] = $list[$k]['comment'];
                }
            } else {
                $list[$k]['comment'] = $list[$k]['comment'];
            }
            $list[$k]['response_username'] = '';
            $list[$k]['response'] = '';
            $list[$k]['response_id'] = '';
            if ($item['pid'] != 0) {
                $fatherCom =  $this->where('id', $item['pid'])->withTrashed()->find();
                $user = User::where('id', $fatherCom->user_id)->find();
                if ($fatherCom && !$fatherCom['delete_time']) {
                    $list[$k]['response_username'] = $user->nickname;
                    // 悬赏模式文章另类处理
                    if (array_key_exists('article_id', $data) && $artData['type'] == 3) {
                        if ($comment_show_type == 2 && $artData->user_id == Session::get('user.user_id')) {
                            $list[$k]['response'] = $fatherCom['comment'];
                        } // 评论者自身评论可见 （作者 ,普通用户）
                        /* 
                        不是评论发布者 Session::get('user.user_id') != $item->user_id 且不是文章发布者 $artData->user_id != $item->user_id
                        */ else if ($comment_show_type == 2 && Session::get('user.user_id') != $fatherCom->user_id) {
                            // 只有发布者的评论所有人可以看 其他人评论不能看
                            if ($artData->user_id != $fatherCom->user_id) {
                                $list[$k]['response'] = '仅发布者可见';
                            } else {
                                $list[$k]['response'] = $fatherCom['comment'];
                            }
                        } else {
                            $list[$k]['response'] = $fatherCom['comment'];
                        }
                    } else {
                        $list[$k]['response'] = $fatherCom['comment'];
                    }
                    $list[$k]['response_id'] = $user['id'];
                } else {
                    $list[$k]['response_username'] = $user->nickname;
                    $list[$k]['response'] = '评论已被作者或系统屏蔽';
                    $list[$k]['response_id'] = $user->id;
                }
            }
        }
        $data = [
            'list' => $list,
            'totalNum' => $list->total()
        ];
        return $data;
    }
}
